$(function() {
	//设置活动按钮
	changeActiveItem('gameList');
	//初始化列表数据
	initTable();
	savehandle();
});


function savehandle() {
	$('#alertdiv').hide();
	$('#alertdiv').savehandle({
		fieldMap : {},
		successMsg : "成功！",
		failMsg : "失败，请重试！",
		success : successCallBack
	});
}

function successCallBack() {
	refresh();
}

/**
 * 初始化表格数据
 */
function initTable() {
	$('#grid').grid({
		title : '',
		pager : true,
		tbar : '#navbar',
		store : {
			url : '/fw/game/game_getlist.action',
			idProperty : 'gameId',
			uploadPagesize : 'rows'
		},
		pageCfg : {
			btnCount : 5
		},
		columns : [
		        {cn:'游戏标题', en:'gameName', width : 100},
		        {cn:'是否发布', en:'push', width : 100, tpl: function(v) {
		        	if(v.push)
		        		return '<input type="checkbox" checked="checked" onclick="isPushClick(' + v.gameId + ',false)">';
		        	else
		        		return '<input type="checkbox" onclick="isPushClick(' + v.gameId + ',true)">';
		        }},
		        {cn:'奖品等级', en:'gameLevel', width : 100},
		        {cn:'游戏次数', en:'gameTime', width : 100},
		        {cn:'中奖基数', en:'fixed', width : 100},
		        {cn:'消耗积分', en:'useGrade', width : 100},
		        {cn:'游戏图片',en:'imgUrl',width:150, tpl:function(v){
					if(v.imgUrl){
						return "<img src='"+v.imgUrl+"' style='width: 75px; height: 75px;'/>";
					}
				}},
				{cn:'游戏介绍',en : 'intro',width : 400,tpl:function(v){
					if(v.intro)
						return '<div style="height: 50px; padding: 2px 0;"><div class="introDiv" style="height: 50px; overflow-y: hidden; cursor: pointer;" onclick="toggleIntro(this);">'+v.intro+'</div></div>';
				}}
				]
	});
}

function isPushClick(gameId, push) {
	$(this)[0].checked = push;
	$.getJSON('/fw/game/game_push.action', {
		'gameId': gameId
	});
}

function formatTime(t) {
	if(t && t.length >= 14)
		return t.substr(0, 4) + '-' + t.substr(4, 2) + '-' + t.substr(6, 2) + ' ' + t.substr(8, 2) + ':' + t.substr(10, 2) + ':' + t.substr(12, 2);
	return t;
}

function add() {
	location.href = "gameAdd.jsp";
}

function edit(){
	var ids = $('#grid').grid('getSelectedIds');
	if(ids.length == 0) {
		$('#alertdiv').savehandle('setmsg',{msg:'请选择要修改的数据!',timer:2000});
		return;
	}else{
		var idsStr = ids[0];
		location.href = "gameEdit.jsp#"+idsStr;
	}
}

function del(){
	var ids = $('#grid').grid('getSelectedIds');
	if(ids.length == 0) {
		$('#alertdiv').savehandle('setmsg',{msg:'请选择要删除的数据!',timer:2000});
		return;
	}else{
		var idsStr = ids.join(',');
		if(confirm("这些数据确定要删除数据？")){
			$.getJSON(
				'/fw/game/game_del.action',
				{
					'ids':idsStr
				},
				function(data){
					$('#alertdiv').savehandle('handle',data,2000);
					refresh();
				}
			);
		}
	}
}

function up() {
	updowntop('up');
}

function down() {
	updowntop('down');
}


function updowntop(type, id) {
	if(!id) {
		var ids = $('#grid').grid('getSelectedIds');
		if(ids.length == 1) {
			id = ids[0];
		} else if(ids.length == 0){
			$('#alertdiv').savehandle('setmsg',{msg:'请选择一条数据!',timer:2000});
			return;
		} else {
			$('#alertdiv').savehandle('setmsg',{msg:'只能选择一条数据!',timer:2000});
			return;
		}
	}
	$.getJSON('/fw/game/game_' + type + '.action', {
		'gameId':id
	}, function(data) {
		$('#alertdiv').savehandle('handle',data,2000);
	});
}



function toggleIntro(div) {
	if($(div).css('overflow-y') == 'hidden')
		$(div).css('overflow-y', 'scroll');
	else
		$(div).css('overflow-y', 'hidden');
}

/**
 * 刷新
 * 
 * @return {TypeName}
 */
function refresh() {
	$('#grid').grid('load', {data : {}});
}

function findAll() {
	refresh();
}